package com.ecr.hub.util;

import javax.faces.application.FacesMessage;
import javax.faces.application.FacesMessage.Severity;
import javax.faces.context.FacesContext;

public class MessageUtils {

	public static void setMessageInfo(String messageKey) {
		setMessage(messageKey, null, FacesMessage.SEVERITY_INFO);
	}

	public static void setMessageError(String messageKey) {
		setMessage(messageKey, null, FacesMessage.SEVERITY_ERROR);
	}

	public static void setMessageWarn(String messageKey) {
		setMessage(messageKey, null, FacesMessage.SEVERITY_WARN);
	}

	public static void setMessageFatal(String messageKey) {
		setMessage(messageKey, null, FacesMessage.SEVERITY_FATAL);
	}

	public static void setMessageInfo(String messageKey, Object[] attributs) {
		setMessage(messageKey, attributs, FacesMessage.SEVERITY_INFO);
	}

	public static void setMessageError(String messageKey, Object[] attributs) {
		setMessage(messageKey, attributs, FacesMessage.SEVERITY_ERROR);
	}

	public static void setMessageWarn(String messageKey, Object[] attributs) {
		setMessage(messageKey, attributs, FacesMessage.SEVERITY_WARN);
	}

	public static void setMessageFatal(String messageKey, Object[] attributs) {
		setMessage(messageKey, attributs, FacesMessage.SEVERITY_FATAL);
	}

	public static FacesMessage getMessageInfo(String messageKey) {

		return getMessage(messageKey, null, FacesMessage.SEVERITY_INFO);
	}

	public static FacesMessage getMessageError(String messageKey) {

		return getMessage(messageKey, null, FacesMessage.SEVERITY_ERROR);
	}

	public static FacesMessage getMessageWarn(String messageKey) {

		return getMessage(messageKey, null, FacesMessage.SEVERITY_WARN);
	}

	public static FacesMessage getMessageFatal(String messageKey) {

		return getMessage(messageKey, null, FacesMessage.SEVERITY_FATAL);
	}

	public static FacesMessage getMessageInfo(String messageKey,
			Object[] attributs) {

		return getMessage(messageKey, attributs, FacesMessage.SEVERITY_INFO);
	}

	public static FacesMessage getMessageError(String messageKey,
			Object[] attributs) {

		return getMessage(messageKey, attributs, FacesMessage.SEVERITY_ERROR);
	}

	public static FacesMessage getMessageWarn(String messageKey,
			Object[] attributs) {

		return getMessage(messageKey, attributs, FacesMessage.SEVERITY_WARN);
	}

	public static FacesMessage getMessageFatal(String messageKey,
			Object[] attributs) {

		return getMessage(messageKey, attributs, FacesMessage.SEVERITY_FATAL);
	}

	public static String getStringMessageError(String messageKey,
			Object[] attributs) {

		return getStringMessage(messageKey, attributs,
				FacesMessage.SEVERITY_ERROR);
	}

	public static String getStringMessageError(String messageKey) {

		return getStringMessage(messageKey, null, FacesMessage.SEVERITY_ERROR);
	}

	public static String getStringMessage(String messageKey, Object[] attributs) {

		return getStringMessage(messageKey, attributs,
				FacesMessage.SEVERITY_INFO);
	}

	public static String getStringMessage(String messageKey) {

		return getStringMessage(messageKey, null, FacesMessage.SEVERITY_INFO);
	}

	public static void setMessageNoBundle(String message) {

		setMessageNoBundle(message, FacesMessage.SEVERITY_INFO);
	}

	public static void setMessageNoBundleError(String message) {

		setMessageNoBundle(message, FacesMessage.SEVERITY_ERROR);
	}

	public static void setMessageNoBundleFatal(String message) {

		setMessageNoBundle(message, FacesMessage.SEVERITY_FATAL);
	}

	public static void setMessage(String messageKey, Object[] attributs,
			Severity severity) {
		String bundleKey;
		if (FacesMessage.SEVERITY_INFO.equals(severity)) {
			bundleKey = "com.ecr.hub.resources.messages";
		} else {
			bundleKey = "com.ecr.hub.resources.errors";
		}
		FacesMessage msg = new FacesMessage(BundleUtils.getMessage(bundleKey,
				messageKey, attributs));
		msg.setSeverity(severity);
		FacesContext.getCurrentInstance().addMessage(messageKey, msg);
	}

	private static FacesMessage getMessage(String messageKey,
			Object[] attributs, Severity severity) {
		String bundleKey;
		if (FacesMessage.SEVERITY_INFO.equals(severity)) {
			bundleKey = "com.ecr.hub.resources.messages";
		} else {
			bundleKey = "com.ecr.hub.resources.errors";
		}
		FacesMessage msg = new FacesMessage(BundleUtils.getMessage(bundleKey,
				messageKey, attributs));

		msg.setSeverity((severity != null) ? severity
				: FacesMessage.SEVERITY_FATAL);

		return msg;
	}

	private static String getStringMessage(String messageKey,
			Object[] attributs, Severity severity) {

		String bundleKey;
		if (FacesMessage.SEVERITY_INFO.equals(severity)) {
			bundleKey = "com.ecr.hub.resources.messages";
		} else {
			bundleKey = "com.ecr.hub.resources.errors";
		}
		return BundleUtils.getMessage(bundleKey, messageKey, attributs);
	}

	private static void setMessageNoBundle(String message, Severity severity) {

		FacesMessage msg = new FacesMessage(message, null);

		msg.setSeverity((severity != null) ? severity
				: FacesMessage.SEVERITY_FATAL);

		FacesContext.getCurrentInstance().addMessage("errMsg", msg);
	}
}
